﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace OpenRPOSMetro
{
    public static class Globals
    {
        // Numeric Constants
        public const double k_dbl_button_spacing = 10.0;
        public const double k_dbl_checkbox_height = 30.0;
        public const double k_dbl_small_checkbox_height = 24.0;
        public const double k_dbl_menu_button_font_size = 28.0;
        public const double k_dbl_menu_small_button_font_size = 20.0;
        public const double k_dbl_nav_button_font_size = 18.0;
        public const double k_dbl_option_group_min_height = 140.0;
        public const double k_dbl_option_group_width = 360;
        public const double k_dbl_option_padding = 3.0;
        public const double k_dbl_radio_button_height = 30.0;
        public const double k_dbl_small_radio_button_height = 20.0;
        public const int k_int_max_options_per_column = 8;
        public const int k_int_max_tables = 99;
        public const int k_int_max_splits = 8;
        public const int k_int_small_rows = 5;
        public const int k_int_status_open = 0;
        public const int k_int_status_sent = 1;
        public const int k_int_status_ready = 2;
        public const int k_int_status_delivered = 3;
        public const int k_int_status_closing = 4;
        public const int k_int_status_closed = 5;

        // String constants
        public const string k_str_pizza_whole = "Whole Pizza";
        public const string k_str_pizza_first_half = "1st Half";
        public const string k_str_pizza_second_half = "2nd Half";
        public const string k_str_settings_show_close_button = "Show Close Button?";
        public const string k_str_settings_show_print_button = "Show Print Button?";
        public const string k_str_value_false = "FALSE";
        public const string k_str_value_no = "NO";
        public const string k_str_value_one = "1";
        public const string k_str_value_true = "TRUE";
        public const string k_str_value_yes = "YES";
        public const string k_str_value_zero = "0";

        // Misc. Data
        public static bool initialized = false;
        public static int loginID;
        public static int token;
        public static ServiceReferenceOpenRPOS.ServiceOpenRPOSClient client = new ServiceReferenceOpenRPOS.ServiceOpenRPOSClient();
        public static ServiceReferenceOpenRPOS.Login currentLogin;
        public static ObservableCollection<ServiceReferenceOpenRPOS.Discount> DiscountRecords = new ObservableCollection<ServiceReferenceOpenRPOS.Discount>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.Login> LoginRecords = new ObservableCollection<ServiceReferenceOpenRPOS.Login>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.MenuCategory> MenuCategoryRecords = new ObservableCollection<ServiceReferenceOpenRPOS.MenuCategory>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.MenuItem> MenuItemRecords = new ObservableCollection<ServiceReferenceOpenRPOS.MenuItem>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.MenuItemOptionGroup> MenuItemOptionGroupRecords = new ObservableCollection<ServiceReferenceOpenRPOS.MenuItemOptionGroup>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.MenuOption> MenuOptionRecords = new ObservableCollection<ServiceReferenceOpenRPOS.MenuOption>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.OptionGroup> OptionGroupRecords = new ObservableCollection<ServiceReferenceOpenRPOS.OptionGroup>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.OrderCheck> OrderCheckRecords = new ObservableCollection<ServiceReferenceOpenRPOS.OrderCheck>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.OrderDetail> OrderDetailRecords = new ObservableCollection<ServiceReferenceOpenRPOS.OrderDetail>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.OrderOption> OrderOptionRecords = new ObservableCollection<ServiceReferenceOpenRPOS.OrderOption>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.OrderSummary> OrderSummaryRecords = new ObservableCollection<ServiceReferenceOpenRPOS.OrderSummary>();

        public static ObservableCollection<ServiceReferenceOpenRPOS.TableLayout> TableLayoutRecords = new ObservableCollection<ServiceReferenceOpenRPOS.TableLayout>();
        public static ObservableCollection<ServiceReferenceOpenRPOS.TableSpecification> TableSpecificationRecords = new ObservableCollection<ServiceReferenceOpenRPOS.TableSpecification>();
    }

    public class CommonUtility
    {
        public  static async Task<decimal> CalculateDiscount(decimal decCurrentTotal, int iDiscountID, int isplitCount=-1)
        {
            decimal dDiscountDollarAmt = 0;
            if (iDiscountID <= 0)
                return decCurrentTotal;

            ServiceReferenceOpenRPOS.Discount discountObject = null;

            if (Globals.DiscountRecords.Where(x => x.col_ID == iDiscountID).Count() == 0)
            {
                ObservableCollection<ServiceReferenceOpenRPOS.Discount> DiscountRecords = await Globals.client.GetDiscountObjectsAsync(Globals.token, "ID=" + iDiscountID.ToString());
                discountObject = DiscountRecords.First();
                Globals.DiscountRecords.Add(discountObject);
            }
            else
                discountObject = Globals.DiscountRecords.Where(x => x.col_ID == iDiscountID).First();


            if (discountObject.col_Amount != 0)
            {
                if (discountObject.col_IsPercent)
                    dDiscountDollarAmt = (-1 * (discountObject.col_Amount / 100) * decCurrentTotal);
                else
                    if (isplitCount > -1) dDiscountDollarAmt = (-1 * discountObject.col_Amount) / isplitCount;  // get Average Amount if it is Dollar Amount and Applied on Whole Order 
                    else dDiscountDollarAmt = (-1 * discountObject.col_Amount);


                decCurrentTotal += dDiscountDollarAmt;
            }

            return decCurrentTotal;
        }

    }

}
